package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.BoxDto;
import com.example.front.dto.BoxSearchDto;
import com.example.front.dto.CraftDto;
import com.example.front.entity.Box;
import com.example.front.entity.Craft;
import com.example.front.entity.Line;
import com.example.front.mapper.BoxMapper;
import com.example.front.mapper.CraftMapper;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CraftService {
    @Resource
    CraftMapper craftMapper;
    @Resource
    LineService lineService;
    @Resource
    BoxService boxService;
    @Resource
    BoxMapper boxMapper;

    /**
     * 添加工艺
     */
    public void createCraft(Craft craft) {
        if (craftMapper.findCraftByName(craft).size() > 0) {
            throw new BusinessException("名称已存在");
        }
        craftMapper.create(craft);
    }

    /**
     * 删除工艺
     */
    public void deleteCraft(Long id) {
        if (craftMapper.delete(UserContext.getUser().getTenantId(), id) == 0) {
            throw new BusinessException("删除失败");
        }
    }


    /**
     * 根据产线删除工艺
     */
    public void deleteCraftByLineId(Long id) {
        if (craftMapper.deleteByLineId(UserContext.getUser().getTenantId(), id) < 0) {
            throw new BusinessException("删除失败");
        }
    }

    /**
     * 工艺信息修改
     */
    @Transactional
    public void updateCraft(Craft craft) {
        craft.setTenantId(UserContext.getUser().getTenantId());

        Box box = new Box();
        box.setCraftId(craft.getId()); //获取工艺id

        if (craft.getBoxId() != null && craft.getBoxId() != 0) {
            box.setId(craft.getBoxId()); //获取盒子id
            Box boxData = boxMapper.findByID(craft.getBoxId()); //找到工艺对应的盒子

            if (boxData != null && boxData.getCraftId() != 0 && !boxData.getCraftId().equals(craft.getId())){
                throw new BusinessException("该按钮盒已绑定工艺");
            }

            boxService.updateByCraftId(box);
        }

        if (craftMapper.update(craft) == 0) {
            throw new BusinessException("操作异常");
        }

    }

    /**
     * 列出工艺列表
     */
    public ListDto<CraftDto> listCraft(Craft condition, Pagination pagination) {
        condition.setTenantId(UserContext.getUser().getTenantId());
        ListDto<CraftDto> list = new ListDto<>();
        // 获取总数
        long total = craftMapper.count(condition);
        // 如果总数为0则直接返回
        if (total == 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }
        pagination.setTotal(total);

        // 获取列表列表并转成Dto
        List<CraftDto> craftDtos = new ArrayList<>();
        List<Craft> crafts = craftMapper.findAll(condition, pagination);
        for (Craft craft : crafts) {
            CraftDto craftDto = new CraftDto();
            BeanUtils.copyProperties(craft, craftDto); // 转dto
            Map<Long, Line> line = lineService.findByIds(Arrays.asList(craftDto.getLineId()));
            if (line.get(craftDto.getLineId()) != null) {
                craftDto.setLineName(line.get(craftDto.getLineId()).getName());
            } else {
                craftDto.setLineName("");
            }
            BoxSearchDto boxSearchDto = new BoxSearchDto();
            boxSearchDto.setCraftId(craft.getId());
            User as = UserContext.getUser();
            boxSearchDto.setTenantId(UserContext.getUser().getTenantId());
            int i = boxService.list(boxSearchDto, pagination).getList().size();
            if (i != 0) {
                BoxDto boxDto = boxService.list(boxSearchDto, pagination).getList().get(0);
                craftDto.setBoxName(boxDto.getName());
                craftDto.setBoxId(boxDto.getId());
            } else {
                craftDto.setBoxName("");
                craftDto.setBoxId(0L);
            }
            craftDtos.add(craftDto);
        }
        // 组装数据
        list.setPagination(pagination);
        list.setList(craftDtos);
        return list;
    }

    /**
     * 查找工艺
     */
    public Craft findById(Long id,long tenantId) {

        Craft craft = craftMapper.findCraftById(tenantId, id);
        if (craft == null) {
            return null;
        }
        return craft;

    }

    /**
     * 查找工艺
     */
    public Craft findById(Long id, Long tenantId) {
        Craft craft = craftMapper.findCraftById(tenantId, id);
        if (craft == null) {
            return null;
        }
        return craft;

    }


    public long count(Craft craft){
        return craftMapper.count(craft);
    }

}
